package com.ssy.lingxi.marketing.handler.convert;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.marketing.model.bo.*;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
 * Jpa 将 Postgresql Json(或Jsonb)字段转换为DiscountBO
 * @author ds
 * @date 2021/6/18
 * @version 2.0.0
 */
@Converter(autoApply = true)
public class JpaJsonToActivityDefinedBOConverter implements AttributeConverter<ActivityDefinedBO, String> {
    private final static ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String convertToDatabaseColumn(ActivityDefinedBO attribute) {
        try {
            return objectMapper.writeValueAsString(attribute);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResponseCode.BUSINESS_ERROR, "权限实体转换Json错误");
        }
    }

    @Override
    public ActivityDefinedBO convertToEntityAttribute(String dbData) {
        try {
            if (dbData == null) {
                return null;
            }
            JSONObject jsonObject1 = JSONUtil.parseObj(dbData);
            Integer type = (Integer)jsonObject1.get("activityType");
            return convert(type,dbData);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResponseCode.BUSINESS_ERROR, "数据库json字段转换错误");
        }
    }

    private ActivityDefinedBO convert(Integer type,String dbData) throws JsonProcessingException {
        ActivityDefinedBO pfDiscountBO = null;
        if(type==null){
            return null;
        }
        switch (type){
            case 1:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<SpecialOfferBO>() {});
                break;
            case 2:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<PlummetBO>() {});
                break;
            case 3:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<DiscountBO>() {});
                break;
            case 4:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<FullQuantityBO>() {});
                break;
            case 5:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<FullMoneyBO>() {});
                break;
            case 6:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<GiveBO>() {});
                break;
            case 7:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<MorePieceBO>() {});
                break;
            case 8:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<CombinationBO>() {});
                break;
            case 9:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<GroupPurchaseBO>() {});
                break;
            case 10:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<LotteryBO>() {});
                break;
            case 11:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<BargainBO>() {});
                break;
            case 12:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<SecKillBO>() {});
                break;
            case 13:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<SwapBO>() {});
                break;
            case 14:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<PreSaleBO>() {});
                break;
            case 15:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<SetMealBO>() {});
                break;
            case 16:
                pfDiscountBO = objectMapper.readValue(dbData, new TypeReference<AttemptBO>() {});
                break;
        }
        return pfDiscountBO;
    }
}
